package com.briup.server;

import com.briup.entity.Environment;
import com.briup.exception.CodeStatus;
import com.briup.exception.EnvException;
import com.briup.interfaces.Configuration;
import com.briup.interfaces.Log;
import com.briup.interfaces.Server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Properties;


/**
 * 服务器端接受功能的逻辑实现
 */
public class ServerImpl implements Server {
    private int port;
    public Log log;

    @Override
    public void init(Properties properties) {
        port = Integer.parseInt(properties.getProperty("port"));
    }

    @Override
    public void setConfiguration(Configuration configuration) {
        log = configuration.getLog();
    }


    @Override
    public Collection<Environment> receive() throws EnvException {
        // 定义服务器
//        log.info("服务器启动，正在监听" + port + "端口...");
        System.out.println("服务器启动，正在监听" + port + "端口...");
        ServerSocket serverSocket = null;
        Socket socket = null;
        ObjectInputStream ois = null;
        Collection collection = new LinkedList();
        try {
            serverSocket = new ServerSocket(port);
            socket = serverSocket.accept();
            // socket中读取信息
            ois = new ObjectInputStream(socket.getInputStream());
            Object obj = ois.readObject();
            // 思考，如何将obj安全的转为Collection集合

            // 先判断集合类型，在判断集合内的元素的类型
            if (obj instanceof Collection<?>) {
                Collection<?> c = (Collection<?>) obj;
                for (Object o : c) {
                    if (o instanceof Environment) {
                        Environment e = (Environment) o;
                        collection.add(e);
                    }
                }
            }

        } catch (IOException e) {
            log.error(e.getMessage());
            throw new EnvException(CodeStatus.IO_EXCEPTION);
        } catch (ClassNotFoundException e) {
            log.error(e.getMessage());
            throw new EnvException(CodeStatus.CLASS_NOT_FOUND);
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                    throw new EnvException(CodeStatus.IO_EXCEPTION);
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                    throw new EnvException(CodeStatus.IO_EXCEPTION);
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    log.error(e.getMessage());
                    throw new EnvException(CodeStatus.IO_EXCEPTION);
                }
            }

        }
        return collection;
    }
}
